package leetcode;

import java.util.ArrayList;
import java.util.List;



public class a530二叉搜索树的最小绝对差 {
    int res=Integer.MAX_VALUE;
    int flag=Integer.MAX_VALUE;

    public int getMinimumDifference(TreeNode root) {
        if (root==null) return 0;
        traversal(root);

        return res;

    }
    public void traversal(TreeNode root){
        if (root==null) return;
        traversal(root.left);
        reduction(root.val);
        traversal(root.right);
    }
    public void reduction(int num){
        int res1=Math.abs(num-flag);
        flag=num;
        if (res1<res){
            res=res1;
        }

    }

    int res1=Integer.MAX_VALUE;
    TreeNode pre;
    public int getMinimumDifference2(TreeNode root) {
        if (root==null) return 0;
        traversal1(root);
        return res1;

    }
    public void traversal1(TreeNode root){
        if (root==null) return;
        traversal1(root.left);
        if (pre!=null){
            res1=Math.min(res1,root.val-pre.val);
        }
        pre=root;
        traversal1(root.right);
    }



}


